package com.reporting.yb.module.system.controller.admin.reporting;

import com.reporting.yb.framework.apilog.core.annotation.ApiAccessLog;
import com.reporting.yb.framework.common.pojo.CommonResult;
import com.reporting.yb.framework.common.pojo.PageParam;
import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;
import com.reporting.yb.framework.excel.core.util.ExcelUtils;
import com.reporting.yb.module.system.controller.admin.reporting.vo.ReportingPageReqVO;
import com.reporting.yb.module.system.controller.admin.reporting.vo.ReportingRespVO;
import com.reporting.yb.module.system.controller.admin.reporting.vo.ReportingSaveReqVO;
import com.reporting.yb.module.system.dal.dataobject.reporting.ReportingDO;
import com.reporting.yb.module.system.dal.dataobject.reportingfile.ReportingFileDO;
import com.reporting.yb.module.system.service.reporting.ReportingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.reporting.yb.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.reporting.yb.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 投诉信息")
@RestController
@RequestMapping("/system/reporting")
@Validated
public class ReportingController {

    @Resource
    private ReportingService reportingService;

    @PostMapping("/create")
    @Operation(summary = "创建投诉信息")
    @PreAuthorize("@ss.hasPermission('system:reporting:create')")
    public CommonResult<String> createReporting(@Valid @RequestBody ReportingSaveReqVO createReqVO) {
        return success(reportingService.createReporting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉信息")
    @PreAuthorize("@ss.hasPermission('system:reporting:update')")
    public CommonResult<Boolean> updateReporting(@Valid @RequestBody ReportingSaveReqVO updateReqVO) {
        reportingService.updateReporting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:reporting:delete')")
    public CommonResult<Boolean> deleteReporting(@RequestParam("id") String id) {
        reportingService.deleteReporting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:reporting:query')")
    public CommonResult<ReportingRespVO> getReporting(@RequestParam("id") String id) {
        ReportingDO reporting = reportingService.getReporting(id);
        return success(BeanUtils.toBean(reporting, ReportingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投诉信息分页")
    @PreAuthorize("@ss.hasPermission('system:reporting:query')")
    public CommonResult<PageResult<ReportingRespVO>> getReportingPage(@Valid ReportingPageReqVO pageReqVO) {
        PageResult<ReportingDO> pageResult = reportingService.getReportingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportingRespVO.class));
    }
    @GetMapping("/getListreporting")
    @Operation(summary = "获得投诉所有信息明细")
    @PreAuthorize("@ss.hasPermission('system:reporting:query')")
    public CommonResult<PageResult<ReportingRespVO>> getListreporting(@RequestParam("category") String category) {
        PageResult<ReportingDO> pageResult = reportingService.getListreporting(category);
        return success(BeanUtils.toBean(pageResult, ReportingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投诉信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:reporting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportingExcel(@Valid ReportingPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportingDO> list = reportingService.getReportingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投诉信息.xls", "数据", ReportingRespVO.class,
                BeanUtils.toBean(list, ReportingRespVO.class));
    }

    // ==================== 子表（投诉文件） ====================

    @GetMapping("/reporting-file/list-by-reporting-id")
    @Operation(summary = "获得投诉文件列表")
    @Parameter(name = "reportingId", description = "投诉id")
    @PreAuthorize("@ss.hasPermission('system:reporting:query')")
    public CommonResult<List<ReportingFileDO>> getReportingFileListByReportingId(@RequestParam("reportingId") String reportingId) {
        return success(reportingService.getReportingFileListByReportingId(reportingId));
    }

}